﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using DbType = Infrastructure.Config.Core.DbType;
namespace Infrasturcture.DapperLight
{
    public class DapperClient
    {
        public DapperLightOption DapperOption { get; set; }
        private IDbConnection _connection;

        public DapperClient(DapperLightOption option)
        {
            DapperOption = option;
        }

        public IDbConnection Connection
        {
            get
            {
                switch (DapperOption.DbType)
                {
                    case DbType.MSSQLSERVER:
                        _connection = new System.Data.SqlClient.SqlConnection(DapperOption.ConnectionString);
                        break;
                    case DbType.ORACLE:
                        break;
                    case DbType.MYSQL:
                        break;
                    case DbType.SQLITE:
                        break;
                    case DbType.MEMORY:
                        break;
                    case DbType.NPGSQL:
                        break;
                    default:
                        throw new Exception("不支持的数据库类型");
                        break;
                }
                return _connection;
            }
        }

        #region 封装部分函数

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <returns></returns>
        public async Task<T> GetAsync<T>(string sql, object param) where T : class
        {
            using (IDbConnection conn = Connection)
            {
                var data = await conn.QuerySingleOrDefaultAsync<T>(sql, param: param);
                return data;
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <returns></returns>
        public async Task<List<T>> GetListAsync<T>(string sql) where T : class
        {
            using (IDbConnection conn = Connection)
            {
                var data = await conn.QueryAsync<T>(sql);
                return data.AsList();
            }
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="paramters"></param>
        /// <returns></returns>
        public async Task<Tuple<int, IEnumerable<T>>> GetPageListAsync<T>(string sql, DynamicParameters paramters)
        {
            using (IDbConnection conn = Connection)
            {
                var data = await conn.QueryMultipleAsync(sql, param: paramters);
                return new Tuple<int, IEnumerable<T>>(await data.ReadFirstAsync<int>(), await data.ReadAsync<T>());
            }
        }

        public async Task<int> GetNumAsync(string sql, DynamicParameters paramters)
        {
            using (IDbConnection conn = Connection)
            {
                var data = await conn.ExecuteScalarAsync<int>(sql, param: paramters);
                return data;
            }
        }
        #endregion
    }
}
